<template>
  <div>
    <div class="filter-container mt-15">
      <el-date-picker v-model="listQuery.datetime[0]" class="filter-item" type="date" placeholder="开始日期"></el-date-picker>
      <el-date-picker v-model="listQuery.datetime[1]" class="filter-item" type="date" placeholder="结束日期"></el-date-picker>
      <el-button class="filter-item" type="primary" plain icon="el-icon-search" @click="handleFilter">搜索</el-button>
      <el-button :loading="downloadLoading" type="primary" icon="el-icon-download" class="filter-item" @click="handleDownload()">导出</el-button>
    </div>
    <el-table ref="table" v-loading="listLoading" :data="list" border stripe style="width: 100%;" height="705">
      <el-table-column label="排序" prop="sort" align="center" fixed width="100"></el-table-column>
      <el-table-column label="打点日志ID" prop="tag" align="center" width="130"></el-table-column>
      <el-table-column label="日志描述" prop="name" align="center" width="180"></el-table-column>
      <el-table-column label="人数" prop="count" align="center" width="100"></el-table-column>
      <el-table-column label="留存比例" prop="ratio" align="center" width="100"></el-table-column>
    </el-table>
  </div>
</template>

<script>
import { getLoadStatisList } from '@/api/actorInfo'

export default {
  name: 'LoadStatis',
  data() {
    return {
      list: [],
      total: 0,
      listLoading: false,
      downloadLoading: false,
      listQuery: {
        datetime: [new Date(), new Date()]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    async getList() {
      const query = this.listQuery
      this.listLoading = true
      const res = await getLoadStatisList(query)
      this.listLoading = false
      this.list = res.data
      this.$nextTick(() => {
        this.$refs.table.doLayout()
      })
    },
    handleFilter() {
      this.getList()
    },
    handleDownload() {
      this.downloadLoading = true
      import('@/vendor/Export2Excel').then(excel => {
        const tHeader = ['排序', '打点日志ID', '日志描述', '人数', '留存比例']
        const filterVal = ['sort', 'tag', 'name', 'count', 'ratio']
        const data = this.formatJson(filterVal)
        excel.export_json_to_excel({
          header: tHeader,
          data,
          filename: '设备统计'
        })
        this.downloadLoading = false
      })
    },
    formatJson(filterVal) {
      return this.list.map(v => filterVal.map(j => {
        return v[j]
      }))
    }
  }
}
</script>
